﻿using Cxuu.Common;
using Cxuu.Models;
using Cxuu.Repository;
using Microsoft.Extensions.DependencyInjection;
using System.Collections.Generic;
using System.Threading.Tasks;

namespace Cxuu.Service
{
    [UseDI(ServiceLifetime.Scoped)]
    public class AdminUserGroupService
    {
        private readonly AdminUserGroupRepository adminGroupDal;
        private readonly AdminUserRepository _adminUser;
        public AdminUserGroupService(AdminUserGroupRepository adminUserGroupRepository, AdminUserRepository adminUser)
        {
            adminGroupDal = adminUserGroupRepository;
            _adminUser = adminUser;
        }

        public async Task<List<AdminUserGroup>> GetListAsync()
        {
            return await adminGroupDal.GetAllListAsync("Id DESC");
        }

        public async Task<AdminUserGroup> GetAsync(int? id)
        {
            return await adminGroupDal.GetAsync(id);
        }

        public async Task<bool> GetByGroupNameAsync(string groupName)
        {
            return await adminGroupDal.GetByGroupNameAsync(groupName);
        }


        public virtual async Task<object> InsertAsync(AdminUserGroup adminUserGroup)
        {
            return await adminGroupDal.InsertAsync(adminUserGroup);
        }


        public async Task<int> UpdateAsync(AdminUserGroup adminUserGroup, int id)
        {
            AdminUserGroup userGroup = await GetAsync(id);
            userGroup.Groupname = adminUserGroup.Groupname;
            userGroup.Systemrole = adminUserGroup.Systemrole;
            userGroup.Channlrole = adminUserGroup.Channlrole;
            return await adminGroupDal.UpdateAsync(adminUserGroup);
        }


        public async Task<bool> DeleteByPriKeyAsync(int id)
        {
            if (await _adminUser.GetByGidAsync(id))
                return false;
            else
                return await adminGroupDal.DeleteByPriKeyAsync(id) > 0;
        }


    }
}
